/**
 * 
 */
package cu.ds.logicalsync.fytasrealm.ui.panel;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import net.jxta.document.Advertisement;
import cu.ds.logicalsync.fytasrealm.communication.NetworkService;
import cu.ds.logicalsync.fytasrealm.communication.model.PeerInfo;

/**
 * @author r.vashira
 * @deprecated
 */
public class PeerListPanel extends JPanel implements Observer {
	private static final long serialVersionUID = 1L;
	
	/*** Member Variables ***/
	private JList peerList;
	private DefaultListModel peerListModel;

	/**
	 * Default Constructor
	 */
	public PeerListPanel() {
		super(new BorderLayout());
		initialize();
	}

	private void initialize() {		
		// Init list model and related components
		peerListModel = new DefaultListModel();
		peerList = new JList(peerListModel);
		peerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		peerList.setSelectedIndex(0);
		peerList.setVisibleRowCount(10);
		
		JScrollPane peerListScrollPane = new JScrollPane(peerList);
		
		this.add(peerListScrollPane, BorderLayout.CENTER);
	}
	
	/**
	 * @deprecated
	 * @param advs
	 */
	private void updatePeerListModel(Enumeration<Advertisement> advs) {
		// clear existing model
		peerListModel.clear();
		
		Advertisement adv = null;
		while (advs.hasMoreElements()) {
            adv = advs.nextElement();
            
            int index = peerList.getSelectedIndex(); //get selected index
            if (index == -1) { //no selection, so insert at beginning
                index = 0;
            } else {           //add after the selected item
                index++;
            }
            
            // insert peer name to the list
            peerListModel.insertElementAt(adv.getID().toString(), index);
        }
	}
	
	private void updatePeerListModel(Set<PeerInfo> allPeers) {
		// clear existing model
		peerListModel.clear();
		
		for(PeerInfo peer : allPeers) {
			peerListModel.addElement(peer.getName());
		}
	}
	
	/**
	 * TODO: Using real unit testing/mocking (JUnit)
	 * @param allMsgMock
	 */
	private void updatePeerListModel(ArrayList<String> allMsgMock) {
		// clear existing model
		peerListModel.clear();
		
		for(String msg : allMsgMock) {
			int index = peerList.getSelectedIndex(); //get selected index
            if (index == -1) { //no selection, so insert at beginning
                index = 0;
            } else {           //add after the selected item
                index++;
            }
            
            // insert peer name to the list
            peerListModel.insertElementAt(msg, index);
		}
	}

	/**
	 * PeerListPanel observe the following Observable
	 * - DiscoveryServiceProvider
	 * - DiscoveryServiceMock (Unit Test for DiscoveryServiceProvider)
	 */
	public void update(Observable o, Object arg) {
		/*if(o instanceof NetworkService) {
			Set<PeerInfo> allPeers = (Set<PeerInfo>) arg;
			updatePeerListModel(allPeers);
		} else if(o instanceof DiscoveryServiceProvider) {
			
			Set<PeerInfo> allPeers = (Set<PeerInfo>) arg;
			updatePeerListModel(allPeers);
		} else if(o instanceof DiscoveryServiceMock) {
			ArrayList<String> allMsgMock = (ArrayList<String>) arg;
			updatePeerListModel(allMsgMock);
		}*/
	}
}
